//package com.example.database_bakup.schedule;
//
//
//import cn.hutool.core.date.DateUnit;
//import cn.hutool.core.date.DateUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.core.env.Environment;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.io.File;
//import java.sql.Connection;
//import java.sql.Date;
//import java.sql.DriverManager;
//import java.sql.Statement;
//import java.text.SimpleDateFormat;
//import java.time.LocalDate;
//import java.util.Map;
//import java.util.logging.Logger;
//
//@Component
//@EnableScheduling
//public class SqlServerBackupTask {
//
//
//    @Autowired
//    private Environment environment;
//
//
//    //每隔30分钟定时执行一次数据库备份
//    @Scheduled(cron = "0/6 * * * * ?")
////    @Scheduled(cron = "0 59 23 * * ?")
//    public void DataBaseBackup() {
//
//        System.out.println("定时任务备份数据库。。。。。。");
////        String databaseName = environment.getProperty("spring.datasource.username");
//        String databaseName = "primaryedu";
//        String databaseUser = environment.getProperty("spring.datasource.username");
//        String databasePassword = environment.getProperty("spring.datasource.password");
//        //注意，文件的存储路径要提前在相应的服务器环境创建好, 看backupCmd的路径如：
//        String databaseBackupUrl = environment.getProperty("backup.url");
//        String serverIp = environment.getProperty("server.ip");
//
//        File file =new File(databaseBackupUrl);
//        if  (!file .exists()  && !file .isDirectory()) {
//            file .mkdirs();
//        }
//
//        try {
//            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//            String url = "jdbc:sqlserver://" + serverIp + ":1433;" + "databaseName=" + databaseName + ";user=" + databaseUser + ";password=" + databasePassword;
//            Connection conn = DriverManager.getConnection(url);
//            Statement stmt = conn.createStatement();
//
////            String backupCmd = "BACKUP DATABASE primaryedu TO DISK = 'D:\\Backup\\primaryedu.bak'";
//            //获取当前时间 yyyy-MM-dd HH:mm:ss
//            java.util.Date date = new java.util.Date();
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//            String currentTime = df.format(date);
//            //拼接成  BACKUP DATABASE primaryedu TO DISK =  'D:\Backup\primaryedu20230615.bak'
//            String backupCmd = "BACKUP DATABASE " + databaseName + " TO DISK =  \'" + databaseBackupUrl + "/" + databaseName +"_" + currentTime + ".bak\'";
//            stmt.execute(backupCmd);
//
//            stmt.close();
//            conn.close();
//
//            System.out.println("Backup successful");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    /**
//     * 定时删除数据库备份文件，保留30天以内的文件
//     */
////    @Scheduled(cron = "0/3 * * * * ?")
//    @Scheduled(cron = "0 59 23 * * ?")
//    public void deleteBackUpDataBase() {
////        Logger.info("定时删除30天以上的数据库备份文件。。。。。。。");
//        String databaseBackupUrl = environment.getProperty("backup.url");;
//        File backUpFile = new File(databaseBackupUrl);
//        if (backUpFile.exists()) {
//            File[] files = backUpFile.listFiles();
//            for (File file : files) {
//                String fileName = file.getName(); //primaryedu_2023-06-28.bak
//                String[] split = fileName.split("_");
//                int length = split.length; //文件名称不对劲
//                if(length == 1) {
//                    file.delete();
//                    continue;
//                }
//                String strDate = split[1].substring(0, 10);
//                java.sql.Date date1 = java.sql.Date.valueOf(strDate);
//                java.sql.Date date2 = java.sql.Date.valueOf(LocalDate.now());
//                long betweenDay = DateUtil.between(date1, date2, DateUnit.DAY);
//                if (betweenDay > 30) {
//                    file.delete();
//                }
//
//            }
//        }
//    }
//
//
//
//
//
//
//}
